import {
  Body,
  Controller,
  Delete,
  Get,
  Inject,
  Param,
  Patch,
  Post,
  Query,
  UsePipes,
  ValidationPipe,
} from '@nestjs/common';
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
import { CreateCoffeeDto } from './dto/create-coffee.dto';
import { UpdateCoffeeDto } from './dto/update-coffee.dto';
import { CoffeeService } from './coffee.service';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
import { Public } from 'src/common/decorators/public.decorator';
import { ParseIntPipe } from 'src/common/pipes/parse-int.pipe';
import { Protocol } from 'src/common/decorators/protocol.decorator';
import { ApiForbiddenResponse, ApiTags } from '@nestjs/swagger';

// @UsePipes(ValidationPipe) // 可以是ValidationPipe 也可以是其实例

@ApiTags('coffee')
@Controller('coffee')
export class CoffeeController {
  constructor(
    private readonly helloService: CoffeeService,
    @Inject(REQUEST) private readonly request: Request,
  ) {
    console.log('create instance in the request');
  }

  @ApiForbiddenResponse({ description: 'forbidden' })
  @Public()
  @Get()
  findAll(
    @Protocol() protocol: string,
    @Query() paginationQuery: PaginationQueryDto,
  ) {
    console.log('protocol', protocol);
    return this.helloService.findAll(paginationQuery);
  }

  @Get(':id')
  async findOne(@Param('id', ParseIntPipe) id) {
    // await new Promise((resolve) => setTimeout(() => resolve(null), 1000));
    return this.helloService.findOne(id);
  }

  @Post()
  create(@Body() createCoffeeDto: CreateCoffeeDto) {
    return this.helloService.create(createCoffeeDto as any);
  }

  @Patch(':id')
  update(
    @Param() { id }: { id: string },
    @Body() updateCoffeeDto: UpdateCoffeeDto,
  ) {
    return this.helloService.update(id, updateCoffeeDto as any);
  }

  @Delete(':id')
  remove(@Param() { id }: { id: string }) {
    return this.helloService.remove(id);
  }
}
